<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="代码" prop="chartimeCode">
        <el-input
          v-model="queryParams.chartimeCode"
          placeholder="请输入代码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="chartimeNames">
        <el-input
          v-model="queryParams.chartimeNames"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['payfee:CharTime:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['payfee:CharTime:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['payfee:CharTime:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['payfee:CharTime:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="CharTimeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="代码" align="center" prop="chartimeCode" />
      <el-table-column label="名称" align="center" prop="chartimeNames" />
      <el-table-column label="说明" align="center" prop="explains" />
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['payfee:CharTime:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['payfee:CharTime:remove']"
          >删除</el-button>
        </template>
      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改收费时间对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="960px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="代码" prop="chartimeCode">
              <el-input v-model="form.chartimeCode" placeholder="请输入代码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="名称" prop="chartimeNames">
              <el-input v-model="form.chartimeNames" placeholder="请输入名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <fieldset class="mb20">
          <legend>收费周期</legend>
          <el-row>
            <el-col :span="8">
              <el-form-item >
               <el-radio-group v-model="form.chargePeriod"  @change="chargePeriodchange()">
                  <el-radio
                    style="margin-bottom: 40px; margin-top: 10px;"
                    v-for="dict in chargePeriodOptions"
                    :key="dict.dictValue"
                    :label="dict.dictValue"
                  >{{dict.dictLabel}}</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="16">
              <el-row>
                <el-col :span="8">
                  <el-form-item label="月数" prop="monthNum">
                    <el-input-number v-model="form.monthNum" min="" :disabled="!form.chargePeriod"></el-input-number>
                  </el-form-item>
                </el-col>
                <!--<el-col :span="24">-->
                  <!--<el-form-item label="所有月份" >-->
                    <!--<el-checkbox-group v-model="form.allMonth" :disabled="dismonthNum">-->
                      <!--<el-checkbox-->
                        <!--v-for="dict in allMonthOptions"-->
                        <!--:key="dict.dictValue"-->
                        <!--:label="dict.dictValue">-->
                        <!--{{dict.dictLabel}}-->
                      <!--</el-checkbox>-->
                    <!--</el-checkbox-group>-->
                  <!--</el-form-item>-->
                <!--</el-col>-->
              </el-row>
            </el-col>
          </el-row>
        </fieldset>
        <fieldset class="mb20">
          <legend>应收日期</legend>
           <el-form-item label="月份">
              <el-radio-group v-model="form.months">
                <el-radio
                  v-for="dict in monthsOptions"
                  :key="dict.dictValue"
                  :label="dict.dictValue"
                >{{dict.dictLabel}}</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="加上" prop="addMonth">
              <el-input-number v-model="form.addMonth"></el-input-number> 个月
            </el-form-item>
            <el-form-item label="日期">
              <el-radio-group v-model="form.datasFormat">
                <el-radio
                  v-for="dict in datasFormatOptions"
                  :key="dict.dictValue"
                  :label="dict.dictValue"
                >{{dict.dictLabel}}</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="每月" prop="perMonth"  v-if="form.datasFormat=='10002'">
              <el-input-number v-model="form.perMonth" :min="1" :max="31" ></el-input-number> 号
            </el-form-item>
            <el-form-item prop="someSky" v-if="form.datasFormat=='10003'">
              <el-input-number v-model="form.someSky"  :max="31" ></el-input-number> 天
            </el-form-item>
        </fieldset>
        <el-form-item label="说明" prop="explains">
          <el-input v-model="form.explains" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCharTime, getCharTime, delCharTime, addCharTime, updateCharTime, exportCharTime } from "@/api/payfee/CharTime";

export default {
  name: "CharTime",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 收费时间表格数据
      CharTimeList: [],
      dismonthNum:true,
      monthNum:true,
      perMonth:true,
      someSky:true,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 收费周期字典
      chargePeriodOptions: [],
      // 所有月份字典
      allMonthOptions: [],
      // 月份字典
      monthsOptions: [],
      // 日期字典
      datasFormatOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        chartimeCode: null,
        chartimeNames: null,
        chargePeriod: null,
        monthNum: null,
        allMonth: null,
        months: null,
        addMonth: null,
        datasFormat: null,
        perMonth: null,
        someSky: null,
        explains: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        chartimeCode: [
          { required: true, message: "代码不能为空", trigger: "blur" }
        ],
        chartimeNames: [
          { required: true, message: "代码不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("charge_period").then(response => {
      this.chargePeriodOptions = response.data;
    });
    this.getDicts("month_type").then(response => {
      this.allMonthOptions = response.data;
    });
    this.getDicts("months").then(response => {
      this.monthsOptions = response.data;
    });
    this.getDicts("datas_format").then(response => {
      this.datasFormatOptions = response.data;
    });
  },
  methods: {
    /** 查询收费时间列表 */
    getList() {
      this.loading = true;
      listCharTime(this.queryParams).then(response => {
        this.CharTimeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 收费周期字典翻译
    chargePeriodFormat(row, column) {
      return this.selectDictLabel(this.chargePeriodOptions, row.chargePeriod);
    },
    // 所有月份字典翻译
    allMonthFormat(row, column) {
      return this.selectDictLabels(this.allMonthOptions, row.allMonth);
    },
    // 月份字典翻译
    monthsFormat(row, column) {
      return this.selectDictLabel(this.monthsOptions, row.months);
    },
    // 日期字典翻译
    datasFormatFormat(row, column) {
      return this.selectDictLabel(this.datasFormatOptions, row.datasFormat);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        chartimId: null,
        chartimeCode: null,
        chartimeNames: null,
        chargePeriod: "10001",
        monthNum: '1',
        allMonth: [],
        months: "10001",
        addMonth: null,
        datasFormat: "10001",
        perMonth: null,
        someSky: null,
        explains: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    chargePeriodchange(){
      if (this.form.chargePeriod == '10001'){
        this.dismonthNum=true
        this.monthNum=false
      }else if (this.form.chargePeriod == '10002'){
        this.dismonthNum=false
        this.monthNum=true
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.chartimId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加收费时间";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const chartimId = row.chartimId || this.ids
      getCharTime(chartimId).then(response => {
        this.form = response.data;
        this.form.allMonth = this.form.allMonth.split(",");
        this.open = true;
        this.title = "修改收费时间";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.allMonth = this.form.allMonth.join(",");
          if (this.form.chartimId != null) {
            updateCharTime(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCharTime(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const chartimIds = row.chartimId || this.ids;
      this.$confirm('是否确认删除收费时间编号为"' + chartimIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCharTime(chartimIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有收费时间数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportCharTime(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
